package 算法题汇总;

import java.util.Comparator;
import java.util.PriorityQueue;

public class findMind {
    private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
    private PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());

    public void addNum(int num){
        if(maxHeap.isEmpty() || maxHeap.peek()>num){
            maxHeap.add(num);
        }else{
            minHeap.add(num);
        }
        int size1 = minHeap.size();
        int size2 = maxHeap.size();
        if(size1-size2 > 1){
            maxHeap.add(minHeap.poll());
        }else if(size2 - size1 >1){
            minHeap.add(maxHeap.poll());
        }
    }
    public double finMid(){
        if(minHeap.size() == maxHeap.size()){
            return (minHeap.poll()+maxHeap.poll())*1.0/2.0;
        } else if (minHeap.size() > maxHeap.size()){
            return minHeap.poll();
        }else{
            return maxHeap.poll();
        }
    }

}
